<template>
  <div style="padding: 15px">
    <el-button type="primary" @click="showSaveChapterDialog">添加章节</el-button>

    <!-- 章节列表 -->
    <ul class="chapterList">
      <li
        v-for="chapter in chapterList"
        :key="chapter.id">
        <p>
          {{ chapter.title }}
          <span class="acts">
            <el-button type="text" @click="showSaveVideoDialog(chapter.id)">添加课时</el-button>
            <el-button type="text" @click="handleEditChapter(chapter.id)">编辑</el-button>
            <el-button type="text" @click="handleDeleteChapter(chapter)">删除</el-button>
          </span>
        </p>
        <!-- 视频 -->
        <ul class="chapterList videoList">
          <li
            v-for="video in chapter.children"
            :key="video.id">
            <p>
              {{ video.title }}
              <el-tag v-if="!video.videoSourceId" size="mini" type="danger">
                {{ '尚未上传视频' }}
              </el-tag>
              <span class="acts">
                <el-tag v-if="video.free" size="mini" type="success">{{ '免费观看' }}</el-tag>
                <el-button type="text" @click="showEditVideoDialog(video)">编辑</el-button>
                <el-button type="text" @click="handleDeleteVideo(video)">删除</el-button>
              </span>
            </p>
          </li>
        </ul>
      </li>
    </ul>

    <div style="text-align: center">
      <el-button :disabled="prevFlag" type="primary" icon="el-icon-arrow-left" @click="prev">上一步</el-button>
      <el-button :disabled="publishFlag" type="primary" @click="next">下一步<i class="el-icon-arrow-right el-icon--right"/></el-button>
    </div>
    <chapter-form ref="chapterDialog"/>
    <video-form ref="videoDialog"/>
  </div>
</template>
<script>
import ChapterForm from '@/views/course/components/CourseChapter/ChapterForm'
import VideoForm from '@/views/course/components/CourseChapter/VideoForm'
import courseApi from '@/api/course'
import videoApi from '@/api/video'
export default {
  components: {
    ChapterForm,
    VideoForm
  },
  data() {
    return {
      publishFlag: false,
      prevFlag: false,
      chapterList: []
    }
  }, created() {
    this.listNestedChapters()
  },
  methods: {
    listNestedChapters() {
      courseApi.listNestedChapters(this.$parent.courseId).then(r => {
        this.chapterList = r.data.items
      })
    },
    handleDeleteVideo(video) {
      this.$confirm(`此操作将永久删除该【${video.title}】, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        videoApi.deleteVideo(video.id).then(r => {
          this.$message.success('删除成功')
          //  刷新页面
          this.listNestedChapters()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    showEditVideoDialog(video) {
      this.$refs.videoDialog.open(video.id)
    },
    handleDeleteChapter(chapter) {
    //  判断要删除的章是否有课时集合，如果有则不能删除
      if (chapter.children && chapter.children.length > 0) {
        this.$message.warning('请先删除章的课时')
        return
      }

      this.$confirm(`此操作将永久删除该【${chapter.title}】, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        courseApi.delete(chapter.id).then(r => {
          this.$message.success('删除成功')
          //  刷新页面
          this.listNestedChapters()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    next() {
      /*
      * 此页面以后在form页面中引入使用
      * form 就是当前页面的父组件
      * info页面就是子组件
      * */
      this.publishFlag = true
      this.$parent.active = 2
    },
    prev() {
      this.prevFlag = true
      this.$parent.active = 0
    },
    showSaveChapterDialog() {
      // 弹出对话框收集章数据
      this.$refs.chapterDialog.dialogFormVisible = true
      this.$refs.chapterDialog.chapter = { sort: 0 }
      this.$refs.chapterDialog.title = '添加章节'
    },
    // 编辑章按钮的单击时间，回显要编辑的章信息到对话框中
    handleEditChapter(id) {
      // 根据id查询章的信息
      this.$refs.chapterDialog.open(id)
    },
    showSaveVideoDialog(chapterId) {
      // 显示新增课时对话框之前，将章的id设置  设置课程和章的id给video
      this.$refs.videoDialog.video = { sort: 0, free: 0 }
      this.$refs.videoDialog.title = '添加课时'

      this.$refs.videoDialog.video.courseId = this.$parent.courseId
      this.$refs.videoDialog.video.chapterId = chapterId
      this.$refs.videoDialog.dialogVisible = true
    }
  }
}
</script>
<style >
.chapterList{
  position: relative;
  list-style: none;
  margin: 0;
  padding: 0;
}
.chapterList li{
  position: relative;
}
.chapterList p{
  float: left;
  font-size: 20px;
  margin: 10px 0;
  padding: 10px;
  height: 70px;
  line-height: 50px;
  width: 100%;
  border: 1px solid #DDD;
}
.chapterList .acts {
  float: right;
  font-size: 14px;
}

.videoList{
  padding-left: 50px;
}
.videoList p{
  float: left;
  font-size: 14px;
  margin: 10px 0;
  padding: 10px;
  height: 50px;
  line-height: 30px;
  width: 100%;
  border: 1px dashed #DDD;
}
</style>
